//
//  TTManagerAddressViewCell.m
//  TaoTuo
//
//  Created by 周晨晔 on 2019/9/20.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTManagerAddressViewCell.h"
#import "TTAddNewAddressViewController.h"
#import "TTAddFactoryAddressViewController.h"

@interface TTManagerAddressViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UIButton *addressBtn;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;

@property (nonatomic, strong) TTAddressModel *model;
@property (nonatomic, assign) BOOL isFactory;


@end

@implementation TTManagerAddressViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self layoutIfNeeded];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    
    self.addressBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);
    self.editBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);
    self.deleteBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);
    
}

- (void)setModel:(TTAddressModel *)model isFactory:(BOOL)isFactory
{
    _model = model;
    self.isFactory = isFactory;
    
    self.nameLabel.text = model.ReceiverName;
    self.phoneLabel.text = model.ReceiverMobile;
    self.addressBtn.selected = model.IsDefault;
    NSString *address = [NSString stringWithFormat:@"%@%@%@%@", model.Province, model.City, model.Area, model.Address];
    self.addressLabel.text = address;
}


// 设为默认
- (IBAction)clickAddress:(UIButton *)sender {
    
    NSString *url = self.isFactory ? setAutoFactoryAddress_api : setDefaultAddress_api;
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:url parameters:@{@"param.receiveAddressId" : self.model.Id} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        if (self.setDefaultAddressBlock) {
            self.setDefaultAddressBlock(self.model);
        }
        
        sender.selected = !sender.selected;
        self.model.IsDefault = sender.selected;
        NSString *str = sender.selected ? @"已设为默认地址" : @"已取消设定默认地址";
        [self.viewController.view makeToast:str duration:1 position:CSToastPositionCenter];
        
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.viewController.view];
    
}

// 编辑
- (IBAction)clickEditBtn:(id)sender {
    if (self.isFactory) {
        TTAddFactoryAddressViewController *newVC = [[TTAddFactoryAddressViewController alloc] init];
        newVC.addressModel = self.model;
        [self.viewController.navigationController pushViewController:newVC animated:YES];
    } else {
        TTAddNewAddressViewController *newVC = [[TTAddNewAddressViewController alloc] init];
        newVC.addressModel = self.model;
        [self.viewController.navigationController pushViewController:newVC animated:YES];
    }
    
}

// 删除
- (IBAction)clickDeleteBtn:(id)sender {
    
    @weakify(self)
    [LLAlertUtilies showAlertWithTitle:@"提示" message:@"确定要删除这个地址吗？" confirmTitle:@"确定" confirmBlock:^{
        @strongify(self)
        [self deleteAddress];
        
    } cancelTitle:@"取消" viewController:self.viewController];
    
}

- (void)deleteAddress
{
    NSString *url = self.isFactory ? deleteFactoryAddress_api : deleteAddress_api;
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:url parameters:@{@"param.receiveAddressId" : self.model.Id} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        if (self.deleteAddressBlock) {
            self.deleteAddressBlock(self);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.viewController.view];
}


@end
